<?php

class Controller_Admin_Qualification_Type extends Controller_Admin {

	public function before() {
		parent::before();
		// set sidebar
		$this->data['sidebar'] = View::forge('admin/items/sidebar');
	}

	public function action_index() {
		$list = Lib\ZList::forge('qualification_types', array(
			'url' => 'admin/qualification/type/index',
			'page' => Input::get('p'),
			'limit' => 18
		));
		if (Input::get('s') != '') {
			$list->fulltext(array( array(
					'name',
					Input::get('s')
				)));

			$list->set_vars(array('s' => Input::get('s')));
		}
		$list->order_by('name');
		$list = $list->get();

		$this->set('zlist', $list);
		$this->set('pagination', $list->pagination);
	}

	public function action_add() {
		if (Input::method() == 'POST') {
			$val = Lib\Validate::forge();
			$val->add_fields(array('name' => array(
					'label' => 'Title',
					'validate' => 'required'
				)));

			if (!$val->run()) {
				Lib\Response::json(array('alert' => $val->get_errors()), true);
			}

			$post = Model_Qualification_Type::forge($val->validated());
			$post->save();

			Lib\Response::json(array(
				'notice' => 'Added successfully',
				'redirect' => Uri::create('admin/qualification/type')
			), true);
		}
	}

	public function action_edit($id = 0) {
		if (Input::method() == 'POST') {
			$val = Lib\Validate::forge();
			$val->add_fields(array(
				'id' => array(
					'validate' => 'required',
					'messages' => array('required' => 'Qualification type does not exists')
				),
				'name' => array(
					'label' => 'Title',
					'validate' => 'required|trim'
				)
			));

			if (!$val->run()) {
				Lib\Response::json(array('alert' => $val->get_errors()), true);
			}

			$data = $val->validated();
			$post = Model_Qualification_Type::find($data['id']);

			if (!$post) {
				Lib\Response::json(array(
					'alert' => 'Qualification does not exists',
					'redirect' => Uri::create('admin/qualification/type')
				), true);
			}

			$post->name = $data['name'];
			$post->save();

			Lib\Response::json(array(
				'notice' => 'Updated successfully',
				'redirect' => Uri::create('admin/qualification/type')
			), true);
		}

		$id = (int)$id;
		$post = Model_Qualification_Type::find($id);

		if (!$post || !$id) {
			Lib\Response::json(array(
				'alert' => 'Not exists',
				'redirect' => Uri::create('admin/qualification/type')
			), true);
		}

		$this->set('post', $post);
	}

	public function action_delete() {
		if (Input::method() == 'POST') {
			$id = (array)Input::post('id');
			if (!$id) {
				Lib\Response::json(array(
					'alert' => 'Not exists',
					'redirect' => Uri::create('admin/qualification/type')
				), true);
			}

			DB::delete('qualification_types')->where('id', 'in', $id)->execute();

			Lib\Response::json(array(
				'notice' => 'Deleted successfully',
				'redirect' => Uri::create('admin/qualification/type')
			), true);
		}
	}

}
